package com.humi.partner.common.entity;

import java.util.Date;
import java.util.List;

import javax.validation.constraints.NotBlank;

import com.alibaba.druid.util.StringUtils;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.humi.cloud.common.annotation.OBSFile;
import com.humi.cloud.common.utils.JsonUtil;
import com.humi.cloud.mybatis.support.model.Entity;
import com.humi.cloud.obs.cache.OBSFileCache;
import com.humi.cloud.utils.IocUtil;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;

/**
 * <pre>
 * <b>伙伴申请审核记录:</b>
 * <b>Author:</b> TianJunmin
 * <b>Date:</b> 2019年10月10日
 * </pre>
 */

@Data
@ApiModel
@NoArgsConstructor
@EqualsAndHashCode(callSuper = false)
@TableName(PartnerApplyRecord.TABLE_NAME)
public class PartnerApplyRecord extends Entity<PartnerApplyRecord> {

	/** **/
	private static final long serialVersionUID = -2279920196434970254L;
	/** 物理表名称. */
	public static final String TABLE_NAME = "partner_apply_record";

	@ApiModelProperty(value = "申请状态(0：审核中；1：审核通过；2：审核驳回重新修改；3：审核驳回不能再提交) ")
	private Integer applyStatus;

	@ApiModelProperty("  审核原因  ")
	private String applyReason;

	@ApiModelProperty("  最后审核时间 ")
	private Date applyTime;

	@ApiModelProperty("  申请记录ID ")
	private String partnerId;

	@TableField(exist = false)
	@ApiModelProperty("附件信息 ")
	private List<Attachment> attachmentList;
	
	@JsonIgnore
	private String attachments;

	@Data
	public static class Attachment {

		/** 附件名称 */
		@NotBlank(message = "附件名称不能为空")
		@ApiModelProperty(value = "附件名称", required = true)
		private String name;

		/** 文件地址 */
		@OBSFile
		@NotBlank(message = "附件名称不能为空")
		@ApiModelProperty(value = "文件地址", required = true)
		private String address;

	}
	
	public String getAttachments() {
		return attachmentList!=null?JsonUtil.toJson(attachmentList):null;
	}
	
	public void setAttachments(String arr) {
		this.attachments=arr;
		if(StringUtils.isEmpty(arr)) {
			return ;
		}
		 List<Attachment> list= JsonUtil.toList(arr, Attachment.class);
		 if(list==null) {
			 return ;
		 }
		 list.forEach(node ->{
			 node.setAddress(IocUtil.getBean(OBSFileCache.class).getFileUrl(node.getAddress()));
		 });
		 attachmentList= list;
	}
}
